/**
 * 向后端请求用户的菜单，动态生成路由
 */
import { constantRouterMap } from "@/config/router.config";
import { generatorDynamicRouter } from "@/router/generator-routers";

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: [],
  },
  mutations: {
    /**
     * 设置路由
     */
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers;
      state.routers = constantRouterMap.concat(routers);
    },
  },
  actions: {
    /**
     * 生成动态的路由
     */
    GenerateRoutes({ commit }, data) {
      return new Promise((resolve) => {
        generatorDynamicRouter(data).then((routers) => {
          commit("SET_ROUTERS", routers);
          resolve();
        });
      }).catch((err) => {
        // eslint-disable-next-line no-undef
        reject(err);
      });
    },
  },
};

export default permission;
